﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.FilterExpression;
using Core.SimpleTemp.Common.PagingQuery;
using Core.Toolkit.ActionResultHelp;
using Core.Toolkit.BaseLibrary.Emit;
using Core.Toolkit.Core.Application;
using Core.Toolkit.PagingQuery;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Core.Toolkit.Core.Api
{
    public static class ControllerBaseExtension
    {
        private static JsonSerializerSettings settings = new JsonSerializerSettings();
        static ControllerBaseExtension()
        {
            settings.ContractResolver = new DefaultContractResolver();
        }
        #region JsonResult

        /// <summary>
        /// 返回带有成功标识的JsonModel
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        static JsonResult JsonSuccessOld(this ControllerBase controllerBase, object data = null)
        {
            return JsonResultHelp.JsonSuccess(data);
        }

        /// <summary>
        /// 返回带有成功标识的JsonModel
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="data"></param>
        /// <param name="isDefaultContractResolver"></param>
        /// <returns></returns>
        public static JsonResult JsonSuccess(this ControllerBase controllerBase, object data = null, bool isDefaultContractResolver = false)
        {
            if (isDefaultContractResolver && data != null)
            {
                return JsonResultHelp.JsonSuccess(data, settings);
            }
            return JsonResultHelp.JsonSuccess(data);
        }

        /// <summary>
        /// 返回带有失败标识的JsonModel
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static JsonResult JsonFaild(this ControllerBase controllerBase, string message = null, object data = null)
        {
            return JsonResultHelp.JsonFaild(message, data);
        }
        #endregion


        public static List<QueryRules> CreateQRList(this ControllerBase controllerBase)
        {
            return new List<QueryRules>();
        }
        public static List<QueryRules> Add(this List<QueryRules> ts, string field, string action)
        {
            ts.Add(new QueryRules() { Field = field, Action = action, });
            return ts;
        }
        public static List<QueryRules> Add(this List<QueryRules> ts, string field, string action, object value)
        {
            ts.Add(new QueryRules() { Field = field, Action = action, Value = value, IsSetValue = true });
            return ts;
        }

        public static Expression<Func<T, bool>> BuildeQR<T>(this List<QueryRules> ts, T dto)
        {
            if (dto == null) return null;
            FilterPagingQueryModel<T> pagingQueryModel = new FilterPagingQueryModel<T>();
            foreach (var item in ts)
            {
                var filterModel = new FilterModel();
                filterModel.Action = item.Action;
                filterModel.Field = item.Field;
                filterModel.Logic = "AND";
                filterModel.Value = item.IsSetValue ? item.Value : PropertyEmit.GetValue(dto, item.Field);
                pagingQueryModel.FilterConditionList.Add(filterModel);
            }
            pagingQueryModel.FilterExpression = ExpressionExtension.GetFilterExpression<T>(pagingQueryModel.FilterConditionList);
            return pagingQueryModel.FilterExpression;
        }
        public static Expression<Func<TEntity, bool>> D<TEntity, TDto>(this List<QueryRules> ts, PagingModel<TDto> pagingModel) where TDto : Dto
        {
            return ts.BuildeQR(pagingModel.Model.ToEntity<TEntity>());
        }
    }
}
